/**
 ********************************************************************************
 *
 *文件名称：
 *
 *文件日期：
 *
 *文件描述：
 *
 *文件版本：
 *
 *修改历史：
 *
 ********************************************************************************
 ***/

/*******************************	头文件包含区	**********************************/
#include "init_config.h"
#include "uart.h"
#include "module_control.h"
#include "sys_clk.h"

/******************************* 全局变量定义区 **********************************/
uint16_t timer_ms = 0;
uint16_t timer_ms_wireless = 0;

#if (PROJECT_TYPEDEF == NB)
extern int16_t count_ota;
#endif

void Timer_delay(void)
{
}

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void delay_us(uint16_t us)
{
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void delay_ms(uint16_t ms)
{
	systick.delay_10ms = 0;
	while (systick.delay_10ms != ms)
	{
		WDT->EN = 0xbb;
	}
}
/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void delay_s(uint16_t s)
{
}

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
extern u8 led1RestartFlag; //刚上电时触发渐变开关灯效果标志

void sys_init(void)
{
	__disable_irq(); //关中断
	syscfg();
	__enable_irq(); //开中断
	sys_gpioInit();
	uart_init(USART_BPS_115200);
	fnRTCDrive_Init();
	tim_init();
	sensor_init(); //计量配置

	//set_light_Lamps1(100);
	delay_ms(50); //防止上电抖动导致reg.RW.restart_count次数乱加
	//LAMP1_SET;
	data_flash_read(10);
	reg.RW.restart_count++;
	data_flash_write();
	restart_init();
	my_cmd_init();
	if (reg.RW.restart_count >= 5)
	{
		if(reg.RW.restart_count == 5)
		{
			reg.RW.ota_flag = 1;
		}
		reg.R.run_state = 0;
		reg.RW.restart_count = 0;
		//data_flash_write();
	}
	if (reg.RW.ota_flag == NONE_OTA)
	{
		/*无OTA升级任务*/
		IAP_APP_Jump();
	}
	else
	{
		if (reg.R.run_state == 1) //如果是从app跳转过来的，不用再重启模组了
		{
			reg.R.run_state = 0; //清零，为了下次重启时初始化模组

			data_flash_write();
			my_queue_init();

			set_rtc_time((reg.RW.year_mon >> 8) & 0xFF,
								(reg.RW.year_mon >> 0) & 0xFF,
								(reg.RW.day_hour >> 8) & 0xFF,
								(reg.RW.day_hour >> 0) & 0xFF,
								(reg.RW.min_sec  >> 8) & 0xFF,
								(reg.RW.min_sec  >> 0) & 0xFF);
						
			sort(timestampTable, reg.W.scene, SCENE_TIME_NUM); //根据时间戳对时间戳和策略时间表重新排序
			
			led1RestartFlag = 2;

			//LOGD("start Erase Flash\r\n");
			FlashErase(FLASH_OTA_ADDR, 512);
			LOGD("Erase Flash end\r\n");
#if (PROJECT_TYPEDEF == NB)
			mainUart.my_data_buf[3] = 21;

			my_cmd_deal(DOWMLINK_READ, APP_PACKsend_request_ota_pack_21, waiting_to_receive, 1, 2000, ACK);
			count_ota = 0;
#elif (PROJECT_TYPEDEF == CAT1)
			my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK06, waiting_to_receive, 1, 1000, ACK);
			my_cmd_deal(AT_QMTPUB_1007, AT_TX_QMTPUB_1007, waiting_to_receive, 1, 2000, ACK);
			my_cmd_deal(AT_QMTPUB_UPLINK, AT_TX_UPLINK06, waiting_to_receive, 1, 1000, ACK);
			my_cmd_deal(AT_QMTPUB_2018, AT_TX_QMTPUB_2018, waiting_to_receive, 1, 2000, ACK);
#elif (PROJECT_TYPEDEF == NB_ONENET)
			// my_cmd_deal(AT_QMTPUB_1007, AT_TX_QMTPUB_1007, waiting_to_receive, 1, 2000, ACK);

			// my_cmd_deal(AT_QMTOPEN, AT_TX_QMTOPEN, waiting_to_receive, 10, 3000, NO_ACK);
			//my_cmd_deal(AT_CCLK, AT_TX_CCLK, AT_RX_CCLK, 1, 2000, ACK);
			my_cmd_deal(AT_QMTPUB_1007, AT_TX_QMTPUB_1007, waiting_to_receive, 1, 2000, ACK);
			my_cmd_deal(AT_QMTPUB_2018, AT_TX_QMTPUB_2018, waiting_to_receive, 1, 2000, ACK);
#endif
			global_control.ack_ok = MY_SET;
			global_control.error_code = AT_DEFAULT;
			global_control.cmd_num = AT_RESTART;
			global_control.error_code_cnt = 0;
			global_control.cmd_timeout = 3000;
			global_control.cmd_delay_time = 200;
			device_time.lamp_data_report_time = 0;
			device_time.else_data_repota_time = 0;
			device_time.at_time = 0;
			reg.RW.ota_flag = OTA_READY;
#if (PROJECT_TYPEDEF == NB || PROJECT_TYPEDEF == NB_ONENET)
			device_info.mqtt_state = NB_IN_INITCFG;
#elif (PROJECT_TYPEDEF == CAT1)
			device_info.mqtt_state = 1;
#endif
		}
		else
			{

				if(!reg.R.upgrade_mark)
				{
					reg.RW.ota_flag = NONE_OTA;
					data_flash_write();
				}
			//FlashReadALL(28, reg.R.device_type, 2);
				set_light_Lamps1(100);
				delay_ms(50); //防止上电抖动导致reg.RW.restart_count次数乱加
				LAMP1_SET;
		}
	}
	sprintf((char *)reg.R.hardware_version, "%s", "EXC-TL1-800E-MOSO-II");
	// PC串口日志打印
	reg.R.software_version = 3;

	PRINT_VERSION;
	logEN = 1;
	swapUint16_t_Plus((u16 *)reg.R.hardware_version, sizeof(reg.R.hardware_version) / 2);
	light_sw_init();
	device_info.e1 = swapUint32_t(reg.R.e1);
}

/**
 *********************************************************************************
 *
 *函数名称：
 *
 *函数参数：无
 *
 *函数返回值：无
 *
 *功能描述：
 *
 *********************************************************************************
 **/
void goto_app_handler(u8 addr)
{
	void (*Int_Entry)(void);
	Int_Entry = (void (*)())(*(uint32_t *)(0x0000F000 + addr));
	(*Int_Entry)();
}
